/*
 * 0655. Add Strings
 * 以字符串的形式给出两个非负整数 num1 和 num2，返回 num1 和 num2 的和。
 * https://www.lintcode.com/problem/add-strings/description
 * 
 * 样例
 * 给定 num1 = "123"，num2 = "45"
 * 返回 "168"
 * 
 * 2018.06.28 @jeyming
 */
package add_strings_0655;

public class Add_strings_0655 {
    /**
     * @param num1: a non-negative integers
     * @param num2: a non-negative integers
     * @return: return sum of num1 and num2
     */
    public static String addStrings(String num1, String num2) {
        // write your code here
    	String addRes = "";
    	int cntNum1 = num1.length() - 1;
    	int cntNum2 = num2.length() - 1;
    	int tmp = 0;
    	while((cntNum1 >= 0) || (cntNum2 >= 0) || tmp > 0) {
    		if((cntNum1 < 0) && (cntNum2 >= 0)) {
    			tmp += num2.charAt(cntNum2--) - '0';
    			addRes += (tmp % 10);
    			tmp /= 10;
    		} else if((cntNum1 >= 0) && (cntNum2 < 0)) {
    			tmp += num1.charAt(cntNum1--) - '0';
    			addRes += (tmp % 10);
    			tmp /= 10;
    		} else if((cntNum1 >= 0) && (cntNum2 >= 0)){
    			tmp += num1.charAt(cntNum1--) + num2.charAt(cntNum2--) - '0' - '0';
    			addRes += (tmp % 10);
    			tmp /= 10;
    		} else {
    			addRes += (tmp % 10);
    			tmp /= 10;
    		}
    	}
    	String ans = "";
    	for(int i = addRes.length() - 1; i >= 0; --i) {
    		ans += addRes.charAt(i);
    	}
    	return ans;
    }

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String ans1 = addStrings("9999999999981","19");
		for(int i = 0; i < ans1.length(); ++i) {
			System.out.print(ans1.charAt(i) + " "); 
		}
	}

}
